<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    //用户注册页面
    public function index()
    {
        return view('login.index');
    }

    //用户登录页面
    public function login()
    {
        $this->validate(request(), [
            'email'  => 'required|email',
            'password'  =>'required|min:6',
            'is_remember'   => 'integer',
        ]);

        $user = request(['email', 'password']);
        $isRemember = boolval(request('is_remember'));
        if (\Auth::attempt($user,$isRemember)) {
            return redirect('/posts');
        }

        return \Redirect::back()->withErrors('邮箱和密码不匹配');
    }

    //登出
    public function logout()
    {
        \Auth::logout();

        return redirect('/login');
    }
}
